package com.mifengs.mall.floor;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.annotation.ItemProviderTag;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.R;
import com.mifengs.mall.adapter.ClassifyRvHorizontalAdapter;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.event.MainEvent;
import com.mifengs.mall.util.ToIntentUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by lihaoqi on 2018/4/20.
 */


@ItemProviderTag(
        viewType = FloorListAdapter.TYPE_FIFTEEN,
        layout = R.layout.item_floor_fifteen
)
public class FloorFifteenProvider extends BaseItemProvider<FloorBean,BaseViewHolder>{


    public FloorFifteenProvider() {
    }

    @Override
    public void convert(BaseViewHolder helper, FloorBean floorBean, int position) {
        RecyclerView rvList =  helper.getView(R.id.rv_list);
        rvList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        final List<AdvertBean> advertBeanList = floorBean.getAdverts();
        ClassifyRvHorizontalAdapter classifyRvAdapter;
        if(advertBeanList.size()>4){
            classifyRvAdapter = new ClassifyRvHorizontalAdapter();
        }else{
            classifyRvAdapter = new ClassifyRvHorizontalAdapter(4);
        }
        classifyRvAdapter.setNewData(advertBeanList);
        rvList.setAdapter(classifyRvAdapter);
        classifyRvAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String appActionUrl  = advertBeanList.get(position).getAppActionUrl();
                if(TextUtils.isEmpty(appActionUrl)){
                    if(advertBeanList.get(position).getActionUrl().indexOf("shareEarn") !=-1){
                        if("".equals(PreferenceUtils.getToken())){
                            EventBus.getDefault().post(new MainEvent(MainEvent.LOGIN));
                        }else{
                            ToIntentUtil.toCommonIntent(advertBeanList.get(position).getActionUrl(),mContext);
                        }
                    }else{
                        ToIntentUtil.toCommonIntent(advertBeanList.get(position).getActionUrl(),mContext);
                    }
                }else{
                    if(Constant.STORE_APPLY.equals(appActionUrl)){
                        EventBus.getDefault().post(new MainEvent(MainEvent.OPENSHOP));
                    }
                }
            }
        });
    }

    @Override
    public void onClick(BaseViewHolder helper, FloorBean floorBean, int position) {

    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, FloorBean data, int position) {
        return false;
    }




}
